﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ATest.Bll.Interfaces;

namespace ATest.Bll.Repositories
{
    public class TestRepository : ITestRepository
    {
        private ATestDataContext _DBContext;
        private IQueryable<Test> _Data;

        public TestRepository()
        {
            _DBContext = new ATestDataContext();
            _Data = _DBContext.Tests.Where(x => !x.Deleted);
        }

        public TestRepository(IQueryable<Test> data)
        {
            _Data = data.Where(x => !x.Deleted);
        }

        public Test GetById(int id)
        {
            return _Data.SingleOrDefault(x => x.Id == id);
        }


        public IQueryable<Test> GetAll()
        {
            return _Data;
        }

        public void SaveChanges()
        {
            if (_DBContext != null)
            {
                _DBContext.SubmitChanges();
            }
        }

        public void Delete(Test test)
        {
            test.SetDeleted(true);
        }

        public void Insert(Test test)
        {
            if (_DBContext != null)
            {
                _DBContext.Tests.InsertOnSubmit(test);
            }
        }
    }
}
